//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.utils
{
	import flash.display.DisplayObject;
	
	public class HTMLCoder
	{
		public static function HTMLImgCode(src:String, width:int = -1, height:int = -1):String
		{
			var w:String = width >= 0 ? " width=\"" + width + "\"" : "";
			var h:String = height >= 0 ? " height=\"" + height + "\"" : "";
			
			var s:String = "<img" + w + h + " src=\"" + src + "\" border=\"0\" />";
			return s;		
		}
		
		public static function UBB2HTML(source:String):String
		{
			var s:String = source;
			s = s.replace(/<TEXTFORMAT.*?>/g,"");
			s = s.replace(/<\/TEXTFORMAT.*?>/g,"");			
			s = s.replace(/\[\/img\]\<\/FONT\>/ig,"[/img]"); 
			s = s.replace(/\[img/ig,"</FONT>[img"); 
			s = s.replace(/\[img=(\d+)\,(\d+)\]\s*(\S+)\[\/img\]/g,'<img src="$3" width="$1" height="$2"/>')
			return s;
		}
		
		public static function HTML2UBB(source:String):String
		{
			var s:String = source;
			return s.replace(/<img.+\"(.+?)\"\D+(\d+)\D+(\d+)\D+\">/g,'[img=$2,$3] $1[/img]');
		}
		
	}
}